<%@page import="com.google.appengine.api.datastore.Query.FilterOperator"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="toledo.rest.*" %>

<html>
  <head>
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
  </head>

  <body>
	<%
	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	List<Entity> users = datastore.prepare(new Query(User.DATABASE)).asList(FetchOptions.Builder.withDefaults());
	List<Entity> courses = datastore.prepare(new Query(Course.DATABASE)).asList(FetchOptions.Builder.withDefaults());
	List<Entity> user_to_course = datastore.prepare(new Query(UserToCourse.DATABASE)).asList(FetchOptions.Builder.withDefaults());
	List<Entity> entries = datastore.prepare(new Query(Entry.DATABASE).addFilter(Entry.REPLY_TO_ID,FilterOperator.EQUAL,"")).asList(FetchOptions.Builder.withDefaults());
	List<Entity> comments = datastore.prepare(new Query(Entry.DATABASE).addFilter(Entry.REPLY_TO_ID,FilterOperator.NOT_EQUAL,"")).asList(FetchOptions.Builder.withDefaults());
	%>
  	<form action="/add_user" method="post">
      <div>username: <input name="<%= User.USERNAME %>"/> - password: <input name="<%= User.PASSWORD %>"/> - twitter_id: <input name="<%= User.TWITTER_ID %>"/><input type="submit" value="New user" /></div>
    </form>
	<%
	if(users.isEmpty())
	{
		%>
		<p>No users atm.</p>
		<%
	}
	else
	{
		%>
		<p>Users:</p>
		<table>
		<tr>
		<th>key</th><th>username</th><th>password</th><th>twitter_id</th>
		</tr>
		<%
		for(Entity user:users)
		{
			String key = KeyFactory.keyToString(user.getKey());
		%>
			<tr>
				<td><%= key %></td>
				<td><%= user.getProperty(User.USERNAME) %></td>
				<td><%= user.getProperty(User.PASSWORD) %></td>
				<td><%= user.getProperty(User.TWITTER_ID) %></td>
			</tr>
		<%
		}
		%>
		</table>
	<%
	}
	%>
	<hr/>
  	<form action="/add_course" method="post">
      <div>course: <input name="<%= Course.COURSENAME %>"/> - hashtag: <input name="<%= Course.HASHTAG %>"/><input type="submit" value="New course" /></div>
    </form>
	<%
	if(courses.isEmpty())
	{
		%>
		<p>No courses atm.</p>
		<%
	}
	else
	{
		%>
		<p>Courses:</p>
		<table>
		<tr>
		<th>key</th><th>coursename</th><th>hashtag</th>
		</tr>
		<%
		for(Entity course:courses)
		{
			String key = KeyFactory.keyToString(course.getKey());
		%>
			<tr>
				<td><%= key %></td>
				<td><%= course.getProperty(Course.COURSENAME) %></td>
				<td><%= course.getProperty(Course.HASHTAG) %></td>
			</tr>
		<%
		}
		%>
		</table>
	<%
	}
	%>
	<hr/>
	<form action="/add_user_to_course" method="post">
      <div>user: <select name="<%= UserToCourse.USER_ID %>"><% for (Entity user:users) { %><option value="<%= KeyFactory.keyToString(user.getKey()) %>"><%= user.getProperty(User.USERNAME) %></option><% } %></select> - course: <select name="<%= UserToCourse.COURSE_ID %>"><% for (Entity course:courses) { %><option value="<%= KeyFactory.keyToString(course.getKey()) %>"><%= course.getProperty(Course.COURSENAME) %></option><% } %></select><input type="submit" value="New user_to_course connection" /></div>
    </form>
    <%
	if(user_to_course.isEmpty())
	{
		%>
		<p>No user/course connections atm.</p>
		<%
	}
	else
	{
		%>
		<p>User/course connections:</p>
		<table>
		<tr>
		<th>user_id</th><th>course_id</th>
		</tr>
		<%
		for (Entity con:user_to_course)
		{
		%>
			<tr>
				<td><%= con.getProperty(UserToCourse.USER_ID) %></td>
				<td><%= con.getProperty(UserToCourse.COURSE_ID) %></td>
			</tr>
		<%
		}
		%>
		</table>
	<%
	}
	%>
	<hr/>
	<form action="/add_entry" method="post">
      <div>user: <select name="<%= Entry.USER_ID %>"><% for (Entity user:users) { %><option value="<%= KeyFactory.keyToString(user.getKey()) %>"><%= user.getProperty(User.USERNAME) %></option><% } %></select> - course: <select name="<%= Entry.COURSE_ID %>"><% for (Entity course:courses) { %><option value="<%= KeyFactory.keyToString(course.getKey()) %>"><%= course.getProperty(Course.COURSENAME) %></option><% } %></select> - text: <input name="<%= Entry.TEXT %>"/><input type="submit" value="New entry" /></div>
    </form>
    <%
	if(entries.isEmpty())
	{
		%>
		<p>No entries atm.</p>
		<%
	}
	else
	{
		%>
		<p>Entries:</p>
		<table>
		
		<tr>
		<th>key</th><th>user_id</th><th>course_id</th><th>date</th><th>type</th><th>text</th>
		</tr>
		<%
		for(Entity entry:entries)
		{
			String key = KeyFactory.keyToString(entry.getKey());
		%>
			<tr>
				<td><%= key %></td>
				<td><%= entry.getProperty(Entry.USER_ID) %></td>
				<td><%= entry.getProperty(Entry.COURSE_ID) %></td>
				<td><%= entry.getProperty(Entry.DATE) %></td>
				<td><%= entry.getProperty(Entry.TYPE) %></td>
				<td><%= entry.getProperty(Entry.TEXT) %></td>
			</tr>
		<%
		}
		%>
		
		</table>
	<%
	}
	%>
	<hr/>
	<form action="/add_comment" method="post">
      <div>user: <select name="<%= Entry.USER_ID %>"><% for (Entity user:users) { %><option value="<%= KeyFactory.keyToString(user.getKey()) %>"><%= user.getProperty(User.USERNAME) %></option><% } %></select> - entry: <select name="<%= Entry.REPLY_TO_ID %>"><% for (Entity entry:entries) { %><option value="<%= KeyFactory.keyToString(entry.getKey()) %>"><%= entry.getProperty(Entry.TEXT) %></option><% } %></select> - text: <input name="<%= Entry.TEXT %>"/><input type="submit" value="New comment" /></div>
    </form>
    <%
	if(comments.isEmpty())
	{
		%>
		<p>No comments atm.</p>
		<%
	}
	else
	{
		%>
		<p>Comments:</p>
		<table>
		
		<tr>
		<th>key</th><th>user_id</th><th>course_id</th><th>date</th><th>reply_to_id</th><th>text</th>
		</tr>
		<%
		for(Entity comment:comments)
		{
			String key = KeyFactory.keyToString(comment.getKey());
		%>
			<tr>
				<td><%= key %></td>
				<td><%= comment.getProperty(Entry.USER_ID) %></td>
				<td><%= comment.getProperty(Entry.COURSE_ID) %></td>
				<td><%= comment.getProperty(Entry.DATE) %></td>
				<td><%= comment.getProperty(Entry.REPLY_TO_ID) %></td>
				<td><%= comment.getProperty(Entry.TEXT) %></td>
			</tr>
		<%
		}
		%>
		
		</table>
	<%
	}
	%>
	<hr/>
	<form action="/add_file" method="post" enctype="multipart/form-data">
      <div>course: <select name="<%= Entry.COURSE_ID %>"><% for (Entity course:courses) { %><option value="<%= KeyFactory.keyToString(course.getKey()) %>"><%= course.getProperty(Course.COURSENAME) %></option><% } %></select> - filename: <input name="title"/> - file: <input type="file" name="filepath"/><input type="submit" value="New file" /></div>
    </form>
  </body>
</html>